<template>
	<view class="">
		<view class="classify" :style="{height:searchHeight+'px'}">
			<!-- 侧边导航栏 -->
			<scroll-view class="nav" scroll-y="true" :scroll-top="scrollLeftTop" scroll-with-animation>
				<view :class="['nav_item', navindex == index ? 'nav_item_choose' : '']" 
				v-for="(item, index) in classList" @tap="changenav(index,item)" :key="index">
					{{item.name}}
					<view class="line">
					</view>
				</view>
			</scroll-view>
			<!-- 右侧商品 -->
			<view class="product">
				<scroll-view scroll-y="true" 
				:scroll-top="scrollTop"
				@scroll="scroll"
				id="scroll_product"
				 class="scroll_product" :style="{height:searchHeight+'px'}">
					<view class="classify_two">
						<view class="product_banner" ref="banner">
							<image :src="prcImage" mode="aspectFill"></image>
						</view>
						<view class="product_classify_title">
							<view class="title">
								{{navList.name}}
							</view>
							<view class="all" @tap="toproductall(navList)">
								全部 <i class="icon">&#xe637;</i>
							</view>
						</view>
						<view class="product_item">
							<view class="product_item_item" @tap="toproduct(items)" v-for="(items, key) in navList.child" :key="key">
								<image :src="items.icon" mode="aspectFill"></image>
								<view class="title">
									{{items.name}}
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props:{
			searchHeight:{
			   type:Number
			},
			classList:{
				type:Array
			}
		},
		watch:{
			classList(newVal,oldVal){
				console.log(JSON.parse(JSON.stringify(newVal)))
				let data = JSON.parse(JSON.stringify(newVal))
				this.prcImage = data[0].icon;
				this.navList = data[0]
				// this.classList = JSON.parse(JSON.stringify(newVal))
			}
		},
		data () {
			return {
				Url:this.Imgurl,
				height: 0,
				navindex: 0,
				scrollTop: 0,
				scrollLeftTop: 0,
				scrollHeight: 0,
				viewTop: 0, //滚动区的可视高度
				leftItemHeight: 51,//49行会计算出新值进行覆盖
				navLeftHeight:0,//左边scroll-view 内层nav的总高度
				diff: 0,//左边scroll-view 内层nav的总高度与视口之差
				tabBarHeight:0,//如果此页面为Tab页面，自己改变高度值,,一般tab高度为51
				arr:[0,584,1168,1752,2336,2805,3274,3858,4442,4911,5380,5734,6203,6672,7017],//初始值，后边计算会根据手机适配覆盖
			    classifyHei:0,
				prcImage:'',
				navList:{}
			}
		},
		created() {
			console.log('66',this.classList)
			// this.prcImage = this.classList[0].icon;
			// this.navList = this.classList[0]
			// console.log(this.classList[0],this.navList)
		},
		mounted() {
			// this.getHeightList();
			this.gettwoclass()
			// console.log('mounted')
			var that = this;
			var version = uni.getSystemInfoSync().version;
			this.$nextTick( ()=> {
				// let that = this;
				// uni.getSystemInfo({
				//     success: function (res) {
				// 		this.classifyHei = res.windowHeight
				// 		// const query = uni.createSelectorQuery()
				// 		// query.select('#search').boundingClientRect(data => {
				// 		// 	that.searchHeight = res.windowHeight - data.height;
				// 		// 	// uni.setStorageSync('searchHeight',data.height)
				// 		// }).exec();
				//     }
				// });
			    // Code that will run only after the
			    // entire view has been rendered
			})
		},
		created() {
		},
		methods: {
			// 获取滚动区域的节点细心
			gettwoclass () {
				let selectorQuery=uni.createSelectorQuery();
				selectorQuery.select('.scroll_product').boundingClientRect(function(re) {
					// console.log(re);
					_this.viewTop = re.height
				})
			},
			changenav (index,item) {
				// console.log(1);
				this.navindex = index;
				this.navList = item;
				this.prcImage = item.icon;
			},
			toproduct (item) {
				uni.navigateTo({
					url: '/pageB/classify/classProduct?name='+item.name+'&id='+item.id
				})
			},
			toproductall (item) {
				uni.navigateTo({
					url: '/pageB/classify/classProduct?name='+item.name+'&id='+item.id
				})
			},
			getHeightList(){
				// console.log(11);
						let _this = this;
						let selectorQuery=uni.createSelectorQuery().in(_this);
						selectorQuery.selectAll('.nav_item').boundingClientRect((rects) => {
							// console.log('nav-item',rects);
							_this.leftItemHeight  =  rects[0].height;
							_this.navLeftHeight = _this.leftItemHeight * _this.navList.length;
							_this.diff =  _this.navLeftHeight - _this.height;
						});
						selectorQuery.selectAll('.classify_two').boundingClientRect(function(rects) {
							// console.log('classify_two',rects);
							let lasttop = rects[rects.length-1].top -rects[rects.length-1].height
							// console.log('classify_two11',lasttop);
							let arr = [0];
							let top = 0;
							rects.forEach(function(rect){
			// 					rect.id      // 节点的ID
			// 					rect.dataset // 节点的dataset
			// 					rect.left    // 节点的左边界坐标
			// 					rect.right   // 节点的右边界坐标
			// 					rect.top     // 节点的上边界坐标
			// 					rect.bottom  // 节点的下边界坐标
			// 					rect.width   // 节点的宽度
			// 					rect.height  // 节点的高度
								top += rect.height;
								// console.log(top);
								arr.push(top)
								})
								arr.pop()
								// console.log(arr);
								_this.arr = arr
								_this.arr[_this.arr.length-1] = _this.arr[_this.arr.length-1] - rects[rects.length-1].height
								// console.log(_this.arr)
							}).exec()
					},
			scroll(e) {
				// console.log(e);
				let _this = this
				if(this.timeoutId){
					clearTimeout(this.timeoutId);
				}
				this.timeoutId = setTimeout(function(){ //节流
					_this.scrollHeight = e.detail.scrollTop +2  + _this.height/2;
					//+1不要删除，解决最后一项某种情况下翻到底部，左边按钮并不会切换至最后一个
					//若想使切换参考线为屏幕顶部请删除 _this.height/2
					for (let i = 0; i < _this.arr.length;i++) {
						let height1 = _this.arr[i];
						let height2 = _this.arr[i+1];
						if (!height2 || (_this.scrollHeight >= height1 && _this.scrollHeight < height2)) {
							// console.log(_this.navindex);
							_this.navindex = i;
							(_this.diff>0) && (_this.scrollLeftTop = Math.round((_this.navindex * _this.diff)/(_this.navList.length-1)));
							return false;
						}
					}
					_this.navindex = 0;
					_this.timeoutId = undefined;
				}, 10)
			},
		}
	}
</script>

<style> 
	page {
		background-color: #FFFFFF;
	}
</style>
<style lang="less" scoped>
	.classify {
		display: flex;
		padding-right: 15upx;
		box-sizing: border-box;
		justify-content: space-between;
	}
	.nav {
		// height:calc(100% - var(--window-bottom));
		margin-right: 10px;
		border-right: 1px solid #F9F9F9;
	}
	.nav_item {
		background-color: #FFFFFF;
		width: 100%;
		height:100upx;
		font-size: 24upx;
		color: #333333;
		padding-left: 30upx;
		box-sizing: border-box;
		line-height:100upx;
	}
	.nav_item_choose {
		// width: 150upx;
		// height: 90upx;
		// line-height: 90upx;
		background-color: #f9f9f9;
		// border-top-right-radius: 8upx;
		// border-bottom-right-radius: 8upx;
		// box-shadow: 10upx 0 10upx 0 #E1E1E1;
		position: relative;
		color: #FF2842;
		font-weight: bold;
		.line {
			width: 6upx;
			height:28upx;
			background-color: #FF2842;
			position: absolute;
			left: 0;
			top: 50%;
			transform: translateY(-50%);
		}
	}
	.product {
		width: 556upx;
		.product_banner {
			width: 556upx;
			height: 164upx;
			image {
				width: 100%;
				height: 100%;
			}
		}
	}
	.product_classify_title {
		margin-top:30upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.title {
			font-size: 28upx;
			color: #000;
			line-height: 28upx;
		}
		.all {
			font-size: 24upx;
			color: #999999;
			line-height: 28upx;
			.icon {
				font-size: 28upx;
				color: #999999;
			}
		}
	}
	.product_item {
		display: flex;
		flex-wrap: wrap;
		.product_item_item {
			width: 33.33%;
			height: 200upx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			align-items: center;
			margin-top: 50upx;
			image {
				width: 160upx;
				height: 160upx;
				display: block;
				margin: auto;
			}
			.title {
				font-size: 24upx;
				color: #000000;
			}
		}
	}
	.scroll_product {
		width: 100%;
		height: 1020upx;
	}
</style>
